package com.micro.ai.template.controller;

import com.micro.ai.commons.domain.ApiResponse;
import com.micro.ai.template.entity.TemplateTag;
import com.micro.ai.template.service.TemplateTagService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 模板标签Controller
 *
 * @author micro-ai
 * @since 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/api/template/tags")
@RequiredArgsConstructor
@Tag(name = "模板标签管理", description = "模板标签相关接口")
public class TemplateTagController {

    private final TemplateTagService templateTagService;

    @PostMapping("/template/{templateId}")
    @PreAuthorize("hasAuthority('template:tag:create')")
    @Operation(summary = "添加标签", description = "为模板添加标签")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "添加成功"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "参数错误"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "401", description = "未授权"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "无权限"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "模板不存在")
    })
    public ApiResponse<Void> addTagsToTemplate(
            @Parameter(description = "模板ID") @PathVariable String templateId,
            @RequestBody List<String> tagNames) {
        log.info("添加标签请求, templateId: {}, tags: {}", templateId, tagNames);
        templateTagService.addTagsToTemplate(templateId, tagNames);
        return ApiResponse.<Void>success(null);
    }

    @DeleteMapping("/template/{templateId}")
    @PreAuthorize("hasAuthority('template:tag:delete')")
    @Operation(summary = "移除标签", description = "从模板移除标签")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "移除成功"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "401", description = "未授权"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "无权限"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "模板不存在")
    })
    public ApiResponse<Void> removeTagsFromTemplate(
            @Parameter(description = "模板ID") @PathVariable String templateId,
            @RequestBody List<String> tagNames) {
        log.info("移除标签请求, templateId: {}, tags: {}", templateId, tagNames);
        templateTagService.removeTagsFromTemplate(templateId, tagNames);
        return ApiResponse.<Void>success(null);
    }

    @GetMapping("/template/{templateId}")
    @Operation(summary = "查询模板标签", description = "获取模板的所有标签")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "查询成功"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "模板不存在")
    })
    public ApiResponse<List<TemplateTag>> getTemplateTags(
            @Parameter(description = "模板ID") @PathVariable String templateId) {
        return ApiResponse.success(templateTagService.getTemplateTags(templateId));
    }

    @GetMapping
    @Operation(summary = "查询所有标签", description = "获取系统中的所有标签")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "查询成功")
    })
    public ApiResponse<List<TemplateTag>> getAllTags() {
        return ApiResponse.success(templateTagService.getAllTags());
    }

    @GetMapping("/popular")
    @Operation(summary = "查询热门标签", description = "获取最热门的标签")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "查询成功")
    })
    public ApiResponse<List<TemplateTag>> getPopularTags(
            @Parameter(description = "返回数量") @RequestParam(defaultValue = "10") int limit) {
        return ApiResponse.success(templateTagService.getPopularTags(limit));
    }

    @GetMapping("/search")
    @Operation(summary = "根据标签搜索", description = "根据标签名称查询关联的模板")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "查询成功")
    })
    public ApiResponse<List<String>> getTemplateIdsByTag(
            @Parameter(description = "标签名称") @RequestParam String tagName) {
        return ApiResponse.success(templateTagService.getTemplateIdsByTag(tagName));
    }
}

